//
//  CZNewFeatureViewController.swift
//  wuJunQianWeiBo
//
//  Created by apple on 15/12/19.
//  Copyright © 2015年 wujunqian. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class CZNewFeatureViewController: UICollectionViewController {
    //流水布局
    private let layout = UICollectionViewFlowLayout()
    //AppDelegate会调用没有参数的构造函数
    init() {
        super.init(collectionViewLayout: layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 属性
    //新特性界面数量
    private let itemNum = 4
    
    override func viewDidLoad() {
        super.viewDidLoad()
        prepareCollectionView()
    }
    
    //MARK: - 准备collectionView的参数
    private func prepareCollectionView()
    {
        //注册cell
        self.collectionView!.registerClass(CZNewFeatrueCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        //设置布局参数，一个item和屏幕一样大
        layout.itemSize = UIScreen.mainScreen().bounds.size
        //不要间距
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        //水平滚动
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        //分页显示
        collectionView?.pagingEnabled = true
        //取消弹簧效果
        collectionView?.bounces = false
    }
    
    
    // MARK: UICollectionViewDataSource
//    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
//        // #warning Incomplete implementation, return the number of sections
//        return itemNum
//    }
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return itemNum
    }
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CZNewFeatrueCell
        
        //这里的item相当于row
        cell.index = indexPath.item
        
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        // collectionView停到某一页,不使用indexPath,我们自己来获取现在正在显示的cell indexPath
        // z正在显示的所有cell的IndexPath
        let visibleIndexPath = collectionView.indexPathsForVisibleItems().last!
        //判断是否是最后一个
        if visibleIndexPath.item == itemNum - 1
        {
            //获取visibleIndexPath对应的cell
            let cell = collectionView.cellForItemAtIndexPath(visibleIndexPath) as! CZNewFeatrueCell
            //开始动画
            cell.startButtonAnimation()
        }
    }
    
}

// MARK: - 自定义cell显示新特性界面
class CZNewFeatrueCell: UICollectionViewCell
{
    // 不同的cell显示不同的图片,别人告诉我们显示哪一页的图片
    // 根据不同的index来设置不同的图片
    var index = 0
        //属性监视器，当index发生变化的时候掉用
    {
        didSet
        {
            //找到对应的图片，设置背景图片
            backImage.image = UIImage(named: "new_feature_\(index + 1)")
            //每当cell显示之前都要隐藏button
            startButton.hidden = true
        }
            
    }
    
    //系统会自动调用
    override init(frame: CGRect) {
        super.init(frame: frame)
        //调用UI
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MRAK: - 开始按钮动画
    func startButtonAnimation()
    {
        //显示按钮
        startButton.hidden = false
        //设置按钮的scale
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        //使用UIView动画
        UIView.animateWithDuration(0.8, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            //清空transform
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                print("开始按钮")
        }
    }
    
    // MARK: - 准备UI
    private func prepareUI() {
        // 添加子控件, TableViewCell和CollectionViewCell,添加子控件需要添加到contentView上面
        contentView.addSubview(backImage)
        contentView.addSubview(startButton)
        
        // 添加约束
        backImage.translatesAutoresizingMaskIntoConstraints = false
        startButton.translatesAutoresizingMaskIntoConstraints = false
        
        // 背景填充父控件
        contentView.addConstraint(NSLayoutConstraint(item: backImage, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        
        contentView.addConstraint(NSLayoutConstraint(item: backImage, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        contentView.addConstraint(NSLayoutConstraint(item: backImage, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        contentView.addConstraint(NSLayoutConstraint(item: backImage, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        // 开始体验按钮约束
        // 水平CenterX和父控件的CenterX重合
        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // 垂直开始按钮的底部和父控件的底部重合
        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
    }
    
    
    //MARK: - 懒加载
    //背景图片
    private lazy var backImage: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    //开始按钮
    private lazy var startButton: UIButton =
    {
        let button = UIButton()
        //设置按钮背景图片
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        //设置按钮文字
        button.setTitle("开始按钮", forState: UIControlState.Normal)
        return button
    }()
    
}


